﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CommunicationCore;
using CommunicationCore.OpcUaClient;
using Opc.Ua;
using Opc.Ua.Client;

namespace PlatesWorkshop.Services
{
    public class KepWareOpcService
    {
        public KepWareOpcService()
        {
            opcClent = new OpcUaSimpleClient();
        }

        private OpcUaSimpleClient opcClent;

        public async Task Connect(string url)
        {
            await opcClent.Connect(url);
        }

        public void CreateSubscription()
        {
            opcClent.CreateSubscription(
                "timeMonitor",
                1000,
                500,
                new List<string> { "i=2258" },
                new List<string> { "time" },
                TimeMonitorHandler
                );
        }

        private void TimeMonitorHandler(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
                if (notification != null)
                {
                    switch (monitoredItem.DisplayName)
                    {
                        case "time":
                            {
                                //kepWareTime.DateTime = notification.Value.Value.ToString();
                                //kepWareTime.SampleCount++;

                                break;
                            }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

       
    }
}
